﻿// Decompiled with JetBrains decompiler
// Type: TaleWorlds.CampaignSystem.CampaignBehaviorDataStore
// Assembly: TaleWorlds.CampaignSystem, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null
// MVID: E85F8C15-4DF6-4E9C-A58A-29177E40D07A
// Assembly location: D:\steam\steamapps\common\Mount & Blade II Bannerlord\bin\Win64_Shipping_Client\TaleWorlds.CampaignSystem.dll

using System.Collections.Generic;
using System.Linq;
using TaleWorlds.Library;
using TaleWorlds.SaveSystem;

#nullable disable
namespace TaleWorlds.CampaignSystem
{
  internal class CampaignBehaviorDataStore
  {
    [SaveableField(1)]
    private readonly Dictionary<string, CampaignBehaviorDataStore.BehaviorSaveData> _behaviorDict;

    internal CampaignBehaviorDataStore()
    {
      this._behaviorDict = new Dictionary<string, CampaignBehaviorDataStore.BehaviorSaveData>();
    }

    internal void SaveBehaviorData(CampaignBehaviorBase campaignBehavior)
    {
      string stringId = campaignBehavior.StringId;
      CampaignBehaviorDataStore.BehaviorSaveData behaviorSaveData = new CampaignBehaviorDataStore.BehaviorSaveData(true);
      campaignBehavior.SyncData((IDataStore) behaviorSaveData);
      if (this._behaviorDict.ContainsKey(stringId))
      {
        Debug.FailedAssert("trying to save multiple behaviors with the same stringid: " + stringId, "C:\\Develop\\MB3\\Source\\Bannerlord\\TaleWorlds.CampaignSystem\\CampaignBehaviorDataStore.cs", nameof (SaveBehaviorData), 75);
        this._behaviorDict[stringId] = behaviorSaveData;
      }
      else
        this._behaviorDict.Add(stringId, behaviorSaveData);
    }

    internal void ClearBehaviorData() => this._behaviorDict.Clear();

    internal void LoadBehaviorData(CampaignBehaviorBase campaignBehavior)
    {
      string stringId = campaignBehavior.StringId;
      CampaignBehaviorDataStore.BehaviorSaveData behaviorSaveData;
      if (this._behaviorDict.TryGetValue(stringId, out behaviorSaveData))
      {
        campaignBehavior.SyncData((IDataStore) behaviorSaveData);
      }
      else
      {
        List<KeyValuePair<string, CampaignBehaviorDataStore.BehaviorSaveData>> list = this._behaviorDict.ToList<KeyValuePair<string, CampaignBehaviorDataStore.BehaviorSaveData>>();
        string name = campaignBehavior.GetType().Name;
        foreach (KeyValuePair<string, CampaignBehaviorDataStore.BehaviorSaveData> keyValuePair in list)
        {
          if (keyValuePair.Key.Contains(name))
          {
            this._behaviorDict.Remove(keyValuePair.Key);
            this._behaviorDict.Add(stringId, keyValuePair.Value);
            campaignBehavior.SyncData((IDataStore) keyValuePair.Value);
            break;
          }
        }
      }
    }

    internal static void AutoGeneratedStaticCollectObjectsCampaignBehaviorDataStore(
      object o,
      List<object> collectedObjects)
    {
      ((CampaignBehaviorDataStore) o).AutoGeneratedInstanceCollectObjects(collectedObjects);
    }

    protected virtual void AutoGeneratedInstanceCollectObjects(List<object> collectedObjects)
    {
      collectedObjects.Add((object) this._behaviorDict);
    }

    internal static object AutoGeneratedGetMemberValue_behaviorDict(object o)
    {
      return (object) ((CampaignBehaviorDataStore) o)._behaviorDict;
    }

    internal class BehaviorSaveData : IDataStore
    {
      [SaveableField(0)]
      private Dictionary<string, object> _records = new Dictionary<string, object>();
      private readonly bool _isSaving;

      public BehaviorSaveData(bool isSaving) => this._isSaving = isSaving;

      public bool SyncData<T>(string key, ref T data)
      {
        if (this.IsSaving)
        {
          this._records.Add(key, (object) data);
          return true;
        }
        object obj;
        if (!this._records.TryGetValue(key, out obj))
          return false;
        data = (T) obj;
        return true;
      }

      public bool IsSaving => this._isSaving;

      public bool IsLoading => !this._isSaving;

      internal static void AutoGeneratedStaticCollectObjectsBehaviorSaveData(
        object o,
        List<object> collectedObjects)
      {
        ((CampaignBehaviorDataStore.BehaviorSaveData) o).AutoGeneratedInstanceCollectObjects(collectedObjects);
      }

      protected virtual void AutoGeneratedInstanceCollectObjects(List<object> collectedObjects)
      {
        collectedObjects.Add((object) this._records);
      }

      internal static object AutoGeneratedGetMemberValue_records(object o)
      {
        return (object) ((CampaignBehaviorDataStore.BehaviorSaveData) o)._records;
      }
    }
  }
}
